package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.UserMapper;
import com.itheima.pojo.Todo;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @ClassName UserServiceImpl
 * @Description 用户表 服务实现类
 * @Author pengkaka
 * @Date 2020/8/4 19:18
 * @Version 1.0
 **/
@Service
public class UserServiceImpl implements UserService {
    
    @Resource
    private UserMapper userMapper;
    
    // 获取用户数
    @Override
    public Integer getUserCount(int status) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status",status);
        Integer count = userMapper.selectCountByExample(example);
        return count;
    }
    
    
    
    // 删除用户的方法
    @Override
    public String deleteUser(User user) {
        user.setStatus(false);
        user.setUpdateTime(LocalDateTime.now());

        int i = userMapper.updateByPrimaryKey(user);
        if (i == 0) {
            return "failed";
        }
        return "succeed";
    }

    // 编辑用户的方法
    @Override
    public String editUser(User user) {
        int i = userMapper.updateByPrimaryKey(user);
        if (i == 0) {
            return "failed";
        }
        return "succeed";
    }

    // 获取用户列表
    // 自作主张不传参数
    @Override
    public Page<User> getUserList() {
        Integer currentPage = 1;
        Integer pageSize = 1;
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        
        PageHelper.startPage(currentPage, pageSize);

        Page<User> pageList = (Page<User>) userMapper.selectAll();
        return pageList;
    }
    
    // 重置用户密码
    @Override
    public String resetUserPassword(User user) {
        int i = userMapper.updateByPrimaryKey(user);
        if (i == 0) {
            return "failed";
        }
        return "succeed";
    }
    
    // 关闭邮件通知状态
    @Override
    public String closeEmailNotification(User user) {
        user.setStartEmailNotification(false);
        int i = userMapper.updateByPrimaryKey(user);
        if (i == 0) {
            return "failed";
        }
        return "succeed";
    }
}
